<?php

chdir(dirname(__FILE__)."/../..");
$path = getcwd();
chdir(dirname(__FILE__));

include_once $path."/../lib/core.class.php";


class PaginatorSnippet extends IPlugin 
{
    function PaginatorSnippet($parent = null)
    {
        IPlugin::IPlugin($parent);
        $this->tplfolder = dirname(__FILE__)."/tpl";
    }

    function getPluginTag() { return "paginator"; }
    function getPluginName() { return "Create Pagination lilnks creation snippet"; }
    function getPluginVersion() { return "1.0.0"; }

    function Render($params = null)
    {
//"cb_class" => &$this,
//"cb_func" => cb_rndCL,
//"urlbase" => "?".$this->getURL()."&page=".$id  // -- base URL to keep
//"caption" => "Subscribers list",
//"header" => array("#", "Name", "Email", "Action"),
//"items" => "",
//"source" => "sql", // -- sql, array
//"sql_items" => "select id, concat(fname,' ',lname) as name, email  from subscribe",
//"sql_count" => "select count(*) from subscribe",
//"ippg" => 10, // -- items per page
//"nppv" => 5, // -- number pages per view
//"page" => &$VAR => return current page
//"ppg" => &$VAR  => return current ppv values


        $core = getCoreInstance();
        $render = $core->getProperty("framework"); $render = $render->smarty;
        
        $render->assign("pg_title", $params["caption"]);
        $render->assign("pg_header", $params["header"]);
        
        $page = $_GET["st_paginator_page"];
        if($page==-1)
        {
            $page = 0;
            $params["ippg"] = 1000000;
        } else 
        if(!($page>=0))
        {
            $page = 0;
        }
        $ippg = $params["ippg"];
        $ppv = $_GET["st_paginator_ppv"];
        if(!($ppv>=0))
        {
            $ppv = 0;
        }
    
        if($ppv>(ceil(($page+1)/$params["nppv"])-1))
        {
            $ppv = ceil(($page+1)/$params["nppv"])-1;
        }

        $dtype = "";
        $dcnt = -1;
        $data = array();
        switch($params["source"])
        {
            case "sql": {
                $dtype = "sql";
                $db = &$core->getProperty("db");
                $dcnt = $db->getOne($params["sql_count"]);
                $data = ArrayDb2Str($db->getAll($params["sql_items"], 0, false, $params["ippg"], $params["ippg"] * $page));
            } break;
            case "array": {
                $dtype = "array";
                $dcnt = count($params["items"]);
                for($i=$params["nppv"]*$page; $i<($params["nppv"]*($page+1)); $i++)
                {
                    if($i>=$dcnt) break;
                    $data[] = $params["items"][$i];
                }
            } break;
        };

        $rdata = array();
        foreach ($data as $row)
        {
            $rdata[] = $params["cb_class"]->$params["cb_func"]($row);
        }

        $render->assign("pg_data", $rdata);
        $render->assign("pg_totals", $dcnt);
        $render->assign("pg_cspan", count($params["header"])-1);
        
        $npg = ceil($dcnt/$params["ippg"]);
        $pages = array();
        for($i=$ppv*$params["nppv"]; $i<($ppv+1)*$params["nppv"]; $i++)
        {
            if($i>=$npg) break;
            $pages[$i+1] = $i == $page ? "" : "?".$params["urlbase"]."&st_paginator_page={$i}&st_paginator_ppv=".(ceil(($i+1)/$params["nppv"])-1);
        }
        $render->assign("pg_pageslist", $pages);
        
        $render->assign("pg_emptymessage", $params["emptymsg"]);
        
        if($page>0 && $ppv>0)
        {
            $render->assign("pg_ppvlist_prev", "?".$params["urlbase"]."&st_paginator_page=".(($ppv*$params["nppv"])-1)."&st_paginator_ppv=".($ppv-1));
        }
        $render->assign("pg_pageslist_prev", $page>0 ? "?".$params["urlbase"]."&st_paginator_page=".($page-1)."&st_paginator_ppv=".((ceil($page/$params["nppv"])-1)<$ppv ? $ppv-1 : $ppv) : "");

        $render->assign("pg_pageslist_next", ($page+1)<$npg ? "?".$params["urlbase"]."&st_paginator_page=".($page+1)."&st_paginator_ppv=".(   (ceil(($page+2)/$params["nppv"])-1)>$ppv ? $ppv+1 : $ppv) : "");

        if(!$npg)
        {
            $nnpg = 0;
        }
        else
        {
            $nnpg = ceil($npg/$params["nppv"])-1;
        }
        if($nnpg>0 && $ppv<$nnpg)
        {
            $render->assign("pg_ppvlist_next", "?".$params["urlbase"]."&st_paginator_page=".(($ppv+1)*$params["nppv"])."&st_paginator_ppv=".($ppv+1));
        }

        $render->assign("pg_pageslist_all", ($npg>0 && $_GET["st_paginator_page"]!=-1) ? "?".$params["urlbase"]."&st_paginator_page=-1" : "" );
        $result = $render->fetch("file:".$this->tplfolder."/page.tpl.php");
        
        // -- return page values
        if(!empty($params["page"]) && !empty($params["ppg"]))
        {
            if (1000000 == $params["ippg"])
            {
                $params["page"] = -1;
                $params["ppg"] = -1;
            }
            else
            {
                $params["page"] = $page;
                $params["ppg"] = $ppv;
            }
        }

        return $result;
    }
};


?>